﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;

namespace phones.ViewModel.TreeViewModel
{
    public class TreeViewItemBaseViewModel : ViewModelBase
    {
        #region Data fields
       // protected string _imageSourceCollapsed = "/View/Images/blue-folder.png";
       // protected string _imageSourceExpanded = "/View/Images/blue-folder-open.png";
        static readonly TreeViewItemBaseViewModel DummyChild = new TreeViewItemBaseViewModel();
        readonly ObservableCollection<TreeViewItemBaseViewModel> _children;
        readonly TreeViewItemBaseViewModel _parent;
        bool _isExpanded;
        bool _isSelected;
        #endregion // Data fields

        #region _CTORs
        protected TreeViewItemBaseViewModel(TreeViewItemBaseViewModel parent, bool lazyLoadChildren)
        {
            _parent = parent;

            _children = new ObservableCollection<TreeViewItemBaseViewModel>();

            if (lazyLoadChildren)
                _children.Add(DummyChild);
        }

        private TreeViewItemBaseViewModel() { }
        #endregion // _CTORs

        #region Presentation
        /// <summary>
        /// Returns the logical child items of this object.
        /// </summary>
        public ObservableCollection<TreeViewItemBaseViewModel> Children
        {
            get { return _children; }
        }

        /// <summary>
        /// Returns true if this object's Children have not yet been populated.
        /// </summary>
        public bool HasDummyChild
        {
            get { return this.Children.Count == 1 && this.Children[0] == DummyChild; }
        }

        /// <summary>
        /// Gets/sets an image for the node
        /// </summary>
        //public string ImageSource
        //{
        //    get { return IsExpanded ? _imageSourceExpanded : _imageSourceCollapsed; }
        //}

        /// <summary>
        /// Gets/sets whether the TreeViewItem 
        /// associated with this object is expanded.
        /// </summary>
        public bool IsExpanded
        {
            get { return _isExpanded; }
            set
            {
                if (value != _isExpanded)
                {
                    _isExpanded = value;
                    RaisePropertyChanged("IsExpanded");
                    RaisePropertyChanged("ImageSource");
                }

                // Expand all the way up to the root.
                if (_isExpanded && _parent != null)
                    _parent.IsExpanded = true;

                // Lazy load the child items, if necessary.
                if (this.HasDummyChild)
                {
                    this.Children.Remove(DummyChild);
                    this.LoadChildren();
                }
            }
        }

        /// <summary>
        /// Gets/sets whether the TreeViewItem 
        /// associated with this object is selected.
        /// </summary>
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value != _isSelected)
                {
                    _isSelected = value;
                    RaisePropertyChanged("IsSelected");
                }
            }
        }
        public TreeViewItemBaseViewModel Parent
        {
            get { return _parent; }
        }

        /// <summary>
        /// Invoked when the child items need to be loaded on demand.
        /// Subclasses can override this to populate the Children collection.
        /// </summary>
        protected virtual void LoadChildren() { }
        #endregion // Presentation
    }
}
